﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
using DotNet.Utilities;

namespace taobaoSpider
{

    public partial class wxFrm : Form
    {
        System.Data.DataSet ds = new DataSet();
        System.Data.DataSet userDS = new DataSet();
        int flag = 0;//标识是新建节点还是重命名事件触发编辑事件。 默认新建为 0 重命名为 1;
        int rowIndex = 0;//选中一行的行号。
        int userRowIndex = 0;
        string exePath = Application.StartupPath;
        public wxFrm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 定义委托类型的事情处理,模仿微软事件处理机制,多线程中用到
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void UpdateDgvValue(object sender, ShowErrorLogEventArgs e)
        {
            WXWS.wx_wsSoapClient client = new WXWS.wx_wsSoapClient();
           // WXWS.wx_wsSoapClient client = new WXWS.wx_wsSoapClient();
            ds=client.GetMpRespMsg();
            //string strSql = "select realToUserName,realFromUserName,subscribe,sex,age,location,userIcon,createTime from MpUserInfo order by createTime desc";
            //userDS = client.GetUserDs(strSql);
            InitTree();
            this.dgv1.DataSource = ds.Tables[0];
        }
        public void VisiteWS()
        {//多线程的解决方案
            ShowErrorLogEventArgs e = new ShowErrorLogEventArgs();
            this.Invoke(new WXUpdateControlValueHandler(UpdateDgvValue), dgv1, e);
        }
        private void wxFrm_Load(object sender, EventArgs e)
        {
            int w = this.Width;
            int h = this.Height;
            dgv1.Width = w - 315;
            dgv1.Height = h - 40;
            user_Dgv.Width = w - 315;
            user_Dgv.Height = h - 40;

            tv1.Height = h - 140;
            btnUser.Location = new Point(0, tv1.Height + 10);
            user_Dgv.Location = new Point(300, 0);

            this.dgv1.Visible = true;
            this.user_Dgv.Visible = false;
            
            // WXWS.wx_wsSoapClient client = new WXWS.wx_wsSoapClient();
            //ds = client.GetMpRespMsg();
            //InitTree();
            //dgv1.DataSource = ds.Tables[0];
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(VisiteWS));
            thread.Start();
        }
        
        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            Point ClickPoint = new Point(e.X, e.Y);
            TreeNode CurrentNode = tv1.GetNodeAt(ClickPoint);
            nodeColor.BackColor = Color.White;
            this.tv1.LabelEdit = false;
            user_Dgv.Visible = false;
            dgv1.Visible = true;
            if (e.Button == MouseButtons.Right)//判断你点的是不是右键
            {
                if (CurrentNode != null)//判断你点的是不是一个节点
                {
                    CurrentNode.ContextMenuStrip = cms_treeView;
                    tv1.SelectedNode = CurrentNode;//选中这个节点
                }
                else
                {
                    tv1.ContextMenuStrip = cms_treeView;
                    tv1.SelectedNode = null;
                }
            }
            if (e.Button == MouseButtons.Left)
            {
                DataTable dt = ds.Tables[0];
                DataView dv = new DataView(dt);
                if (CurrentNode != null)
                {
                    if (CurrentNode.GetNodeCount(true) == 0)
                    {//对于没有子节点的用id_auto来标识，用添加父节点在DGV中
                        if (CurrentNode.Name != "")
                        {
                            if (CurrentNode.Parent != null)
                            {
                                dv.RowFilter = "id_auto='" + CurrentNode.Name + "' or id_auto='" + CurrentNode.Parent.Name + "'"; //"r1='" + CurrentNode.Name + "' or id_auto='" + CurrentNode.Name + "'";
                            }
                            else
                            {
                                dv.RowFilter = "id_auto='" + CurrentNode.Name + "'";
                            }
                            dv.Sort = "keyword asc";
                        }
                    }
                    else
                    {
                        if (CurrentNode.Name != "")
                        {
                            // "r1=0 or id_auto=2;
                            dv.RowFilter = "r1='" + CurrentNode.Name + "' or id_auto='"+CurrentNode.Name+"'"; //添加过滤条件.过滤出一级菜单
                            dv.Sort = "keyword asc";
                        }
                    }
                }
                dgv1.DataSource = dv;
            }
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode node = tv1.SelectedNode;
            tv1.LabelEdit = true;
            flag = 0;
            //if (tv1.Nodes.Count <= 9)
            //{
            if (node == null) //表明是一级目录(最多9项)
            {
                TreeNode tmpNode = this.tv1.Nodes.Add("新建一级目录");
                tmpNode.BeginEdit();
            }
            else  //是子二级子目录(最多9项)
            {
                if (node.GetNodeCount(true) == 0)//添加结点时没有子结点则先添加，再展开。
                {//先添加一个结点，然后将节点下的添加节点展开，然后对添加节点编辑
                    if (node.Level == 0)
                    {
                        TreeNode tmpNode = node.Nodes.Add("新建二级目录");
                        node.Expand();
                        tmpNode.BeginEdit();
                    }
                    if (node.Level == 1)
                    {
                        TreeNode tmpNode = node.Nodes.Add("新建子项");
                        node.Expand();
                        tmpNode.BeginEdit();
                    }
                }
                else//添加结点前展开结点树(有子节点则展开)
                {
                    node.Expand();
                    TreeNode tmpNode=node.Nodes.Add("新建二级目录");
                    tmpNode.BeginEdit();
                }
            }
            //}
            //else //多于9项不能再创建
            //{
            //    MessageBox.Show("一级目录最多只能有9项，多余项会影响回复消息问题");
            //}
        }

        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            WXWS.MpRespMsg rspEntity = new WXWS.MpRespMsg();
            WXWS.wx_wsSoapClient client = new WXWS.wx_wsSoapClient();
            try
            {
                if (flag == 0)
                {
                    if (e.Node.Parent == null) //是新建根节点节点
                    {
                        if (e.Node.PrevNode != null)
                        {
                            string strKeyWord = e.Node.PrevNode.Tag.ToString();
                            string pattern = @"^\d+$";
                            Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
                            MatchCollection matches = rgx.Matches(strKeyWord);
                            if (matches.Count > 0)
                            {
                                rspEntity.keyword = (Convert.ToInt32(strKeyWord) + 1).ToString();
                            }
                            else
                            {
                                rspEntity.keyword = "999999";  //如果非数字关键词
                            }
                        }
                        else
                        {
                            //创建结点时没有对Tag赋值，故不能这样用。rspEntity.keyword = (e.Node.Parent.Tag.ToString() + "1").ToString();
                            rspEntity.keyword = (e.Node.Parent.Tag.ToString() + "1").ToString();
                        }
                        rspEntity.r1 = "0";  //没有父的根节点为0
                        rspEntity.msgType = "news";
                        rspEntity.news_Title = e.Node.Text;
                        e.Node.Tag = rspEntity.keyword;
                    }
                    else
                    {
                        if (e.Node.PrevNode != null)
                        {
                            string strKeyWord = e.Node.PrevNode.Tag.ToString();
                            string pattern = @"^\d+$";
                            Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
                            MatchCollection matches = rgx.Matches(strKeyWord);
                            if (matches.Count > 0)
                            {
                                rspEntity.keyword = (Convert.ToInt32(strKeyWord) + 1).ToString();
                            }
                            else
                            {
                                rspEntity.keyword = "999999";  //如果非数字关键词
                            }
                        }
                        else
                        {
                            rspEntity.keyword = (e.Node.Parent.Tag.ToString() + "1").ToString();
                        }
                        rspEntity.r1 = e.Node.Parent.Name;
                        rspEntity.news_Title = e.Node.Text;
                        rspEntity.msgType = "news";
                        e.Node.Tag = rspEntity.keyword;
                        //dgvr.Cells["news_Title"].Value = e.Node.Text;
                        //dgvr.Cells["msgType"].Value = "news";
                    }
                    client.InsertNode(rspEntity);//插入成功后再读取节点的auto_id作为其名字，插入节点时keyword是唯一的。
                    string xx = "";
                    e.Node.Name = client.GetIdAuto(out xx,rspEntity.keyword).ToString();
                    rspEntity.id_auto = Convert.ToInt32(e.Node.Name);
                    rspEntity.r2 = xx;
                    InserRowDs(rspEntity);
                }
                if (flag == 1)
                {

                }
                //ds = client.GetMpRespMsg();这样对性能有影响，在DGV中插入一项
                //dgv1.DataSource = ds.Tables[0];
                flag = 2;//编辑完成了
            }
            catch (Exception ex)
            {
                GlobalFun.PrintExceptionLog(ex);
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {//删除结点判断是否有子项，有子项目不能删除
            if (MessageBox.Show("你是否确认删除","",MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                TreeNode node = tv1.SelectedNode;
                if (node != null)
                {
                    if (node.GetNodeCount(true) == 0)
                    {
                        WXWS.MpRespMsg rspEntity = new WXWS.MpRespMsg();
                        WXWS.wx_wsSoapClient client = new WXWS.wx_wsSoapClient();
                        if (node.Name != "")
                        {
                            tv1.SelectedNode.Remove();
                            rspEntity.id_auto = Convert.ToInt32(node.Name);
                            client.DeleteNode(rspEntity.id_auto);
                            DeleteLocalDataTable(rspEntity.id_auto);
                        }
                        else
                        {
                            //MessageBox.Show("新加入的结点要重新加载后才可以删除");
                        }
                        
                        //ds = client.GetMpRespMsg();此处改为本地删除
                        //dgv1.DataSource = ds.Tables[0];
                    }
                    else
                    {
                        MessageBox.Show("含有子项不能删除");
                    }
                }
            }
        }

        private void 重命名ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            flag = 1;
            tv1.LabelEdit = true;
            tv1.SelectedNode.BeginEdit();
        }

        private void InitTree()
        {
            DataTable dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.RowFilter = "r1=0"; //添加过滤条件.过滤出一级菜单
            //tv1.ShowCheckBoxes = TreeNodeTypes.All;
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                if (drv["msgType"].ToString() == "text")
                    node.Text = drv["text_Content"].ToString();
                else
                    node.Text = drv["news_Title"].ToString();
                node.Name = drv["id_auto"].ToString();
                node.Tag = drv["keyword"].ToString(); //额外的标识
                //node.Expanded = false;
                tv1.Nodes.Add(node);
                AddReplies(dt, node);
            }
        }

        private void AddReplies(DataTable dt, TreeNode node)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "r1='" + node.Name + "'";
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                if (row["msgType"].ToString() == "text")
                    replyNode.Text = row["text_Content"].ToString();
                else
                    replyNode.Text = row["news_Title"].ToString();
                replyNode.Name = row["id_auto"].ToString();
                replyNode.Tag = row["keyword"].ToString();
               // replyNode.Expanded = false;
                node.Nodes.Add(replyNode);
                AddReplies(dt, replyNode);
            }
        }
        private List<TreeNode> GetAllTreeNode(TreeNodeCollection nodes)
        {
            List<TreeNode> node = new List<TreeNode>();
            foreach (TreeNode tNode in nodes)
            {
                if (tNode.GetNodeCount(false) == 0)
                {
                    node=GetAllTreeNode(tNode.Nodes);
                }
            }
            return node;
        }
        TreeNode nodeColor = new TreeNode(); //标识当前有颜色的节点
        TreeNode nodeZD = new TreeNode();//标识折叠的节点;
        private void dgv1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            rowIndex = e.RowIndex;
            try
            {
                if (nodeColor.Name != "")
                {
                    nodeColor.BackColor = Color.White;
                }
                if (nodeZD != null)
                {
                    nodeZD.Collapse();
                }
                if (rowIndex != -1)
                {
                    if (e.Button == MouseButtons.Left)
                    {
                        //GetAllTreeNode(this.tv1.Nodes);
                        //TreeNode node = new TreeNode();
                        TreeNode[] nodes = this.tv1.Nodes.Find(this.dgv1.Rows[rowIndex].Cells["id_auto"].Value.ToString(), true);
                        TreeNode nodeParent = nodes[0].Parent;  //父节点初始化.用来找到顶层节点.
                        TreeNode currentNode = new TreeNode();  //要展开的父节点.
                        while (nodeParent != null)
                        {
                            currentNode = nodeParent;
                            nodeParent = nodeParent.Parent;

                        }
                        currentNode.ExpandAll();
                        nodeZD = currentNode;
                        nodes[0].BackColor = Color.Red;
                        nodeColor = nodes[0];
                    }
                    if (e.Button == MouseButtons.Right)
                    {
                        dgv1.Rows[e.RowIndex].ContextMenuStrip = cms_Dgv;
                    }
                }
            }
            catch (Exception ex)
            {
                GlobalFun.PrintExceptionLog(ex);
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WXWS.MpRespMsg resp = new WXWS.MpRespMsg();
            resp.id_auto = (int)dgv1.Rows[rowIndex].Cells["id_auto"].Value;
            resp.keyword = (string)dgv1.Rows[rowIndex].Cells["keyword"].Value;
            resp.msgType = (string)dgv1.Rows[rowIndex].Cells["msgType"].Value;
            resp.text_Content = dgv1.Rows[rowIndex].Cells["text_Content"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["text_Content"].Value;
            resp.news_Title = dgv1.Rows[rowIndex].Cells["news_Title"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["news_Title"].Value;
            resp.news_Description = dgv1.Rows[rowIndex].Cells["news_Description"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["news_Description"].Value;
            resp.news_PicUrl = dgv1.Rows[rowIndex].Cells["news_PicUrl"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["news_PicUrl"].Value;
            resp.r3 = dgv1.Rows[rowIndex].Cells["r3"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["r3"].Value;
            resp.news_Url = dgv1.Rows[rowIndex].Cells["news_Url"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["news_Url"].Value;
            resp.r1 = (string)dgv1.Rows[rowIndex].Cells["r1"].Value;
            resp.music_HQMusicUrl = dgv1.Rows[rowIndex].Cells["music_HQMusicUrl"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["music_HQMusicUrl"].Value;
            resp.music_MusicUrl = dgv1.Rows[rowIndex].Cells["music_MusicUrl"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["music_MusicUrl"].Value;
            resp.r2 = dgv1.Rows[rowIndex].Cells["r2"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["r2"].Value;

            wxXgFrm xgFrm = new wxXgFrm(resp);
            if (DialogResult.OK == xgFrm.ShowDialog())
            {
                xgFrm.Close();
                UpdateDgv1(resp);
                if (xgFrm.MsgType == "text")
                {
                    this.tv1.Nodes.Find(resp.id_auto.ToString(), true)[0].Text = xgFrm.TextContent;
                }
                else
                {
                    this.tv1.Nodes.Find(resp.id_auto.ToString(), true)[0].Text = xgFrm.NewsTitle;
                }
            }

        }
        private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定要删除", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                int auto_id = (int)dgv1.Rows[rowIndex].Cells["id_auto"].Value;
                WXWS.wx_wsSoapClient client = new WXWS.wx_wsSoapClient();
                client.DeleteNode(auto_id);
                dgv1.Rows.Remove(dgv1.Rows[rowIndex]);
                tv1.Nodes.RemoveByKey(auto_id.ToString());
            }
        }
        private void dgv1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            WXWS.MpRespMsg resp = new WXWS.MpRespMsg();
            resp.id_auto = (int)dgv1.Rows[rowIndex].Cells["id_auto"].Value;
            resp.keyword = (string)dgv1.Rows[rowIndex].Cells["keyword"].Value;
            resp.msgType = (string)dgv1.Rows[rowIndex].Cells["msgType"].Value;
            resp.text_Content = dgv1.Rows[rowIndex].Cells["text_Content"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["text_Content"].Value;
            resp.news_Title = dgv1.Rows[rowIndex].Cells["news_Title"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["news_Title"].Value;
            resp.news_Description = dgv1.Rows[rowIndex].Cells["news_Description"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["news_Description"].Value;
            resp.news_PicUrl = dgv1.Rows[rowIndex].Cells["news_PicUrl"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["news_PicUrl"].Value;
            resp.r3 = dgv1.Rows[rowIndex].Cells["r3"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["r3"].Value;
            resp.news_Url = dgv1.Rows[rowIndex].Cells["news_Url"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["news_Url"].Value;
            resp.r1 = (string)dgv1.Rows[rowIndex].Cells["r1"].Value;
            resp.music_HQMusicUrl = dgv1.Rows[rowIndex].Cells["music_HQMusicUrl"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["music_HQMusicUrl"].Value;
            resp.music_MusicUrl = dgv1.Rows[rowIndex].Cells["music_MusicUrl"].Value == DBNull.Value ? "" : (string)dgv1.Rows[rowIndex].Cells["music_MusicUrl"].Value;
            resp.r2 = dgv1.Rows[rowIndex].Cells["r2"].Value == DBNull.Value ? "" : dgv1.Rows[rowIndex].Cells["r2"].Value.ToString();

            wxXgFrm xgFrm = new wxXgFrm(resp);
            if (DialogResult.OK == xgFrm.ShowDialog())
            {
                xgFrm.Close();
                UpdateDgv1(resp);
                if (xgFrm.MsgType == "text")
                {
                    this.tv1.Nodes.Find(resp.id_auto.ToString(), true)[0].Text = xgFrm.TextContent;
                }
                else
                {
                    this.tv1.Nodes.Find(resp.id_auto.ToString(), true)[0].Text = xgFrm.NewsTitle;
                }
            }
        }

        private void UpdateDgv1(WXWS.MpRespMsg respMsg)
        {
            //dgv1.Rows[rowIndex].Cells["id_auto"].Value = respMsg.id_auto;
            dgv1.Rows[rowIndex].Cells["keyword"].Value = respMsg.keyword;
            //dgv1.Rows[rowIndex].Cells["r1"].Value = respMsg.r1;
            dgv1.Rows[rowIndex].Cells["msgType"].Value = respMsg.msgType;
            dgv1.Rows[rowIndex].Cells["text_Content"].Value = respMsg.text_Content;
            dgv1.Rows[rowIndex].Cells["news_Title"].Value = respMsg.news_Title;
            dgv1.Rows[rowIndex].Cells["news_Description"].Value = respMsg.news_Description;
            dgv1.Rows[rowIndex].Cells["news_PicUrl"].Value = respMsg.news_PicUrl;
            dgv1.Rows[rowIndex].Cells["r3"].Value = respMsg.r3;
            dgv1.Rows[rowIndex].Cells["news_Url"].Value = respMsg.news_Url;
            dgv1.Rows[rowIndex].Cells["music_MusicUrl"].Value = respMsg.music_MusicUrl;
            dgv1.Rows[rowIndex].Cells["music_HQMusicUrl"].Value = respMsg.music_HQMusicUrl;
            dgv1.Rows[rowIndex].Cells["r2"].Value = respMsg.r2;
        }

        /// <summary>
        /// 在本地DataSet中添加一行数据，减少到网络的访问开销
        /// </summary>
        private void InserRowDs(WXWS.MpRespMsg respMsg)
        {
            DataRow newRow = ds.Tables[0].NewRow();
            try
            {
                newRow["id_auto"] = respMsg.id_auto;
                newRow["keyword"] = respMsg.keyword;
                newRow["r1"] = respMsg.r1;
                newRow["msgType"] = respMsg.msgType;
                newRow["text_Content"] = respMsg.text_Content;
                newRow["news_Title"] = respMsg.news_Title;
                newRow["news_Description"] = respMsg.news_Description;
                newRow["news_PicUrl"] = respMsg.news_PicUrl;
                newRow["r3"] = respMsg.r3;
                newRow["news_Url"] = respMsg.news_Url;
                newRow["music_MusicUrl"] = respMsg.music_MusicUrl;
                newRow["music_HQMusicUrl"] = respMsg.music_HQMusicUrl;
                newRow["r2"] = respMsg.r2;
                ds.Tables[0].Rows.Add(newRow);
            }
            catch (Exception ex)
            {
                GlobalFun.PrintExceptionLog(ex);
            }
        }

        private void DeleteLocalDataTable(int auto_id)
        {
            DataRow[] arrRows = ds.Tables[0].Select("id_auto='"+auto_id.ToString()+"'");
            ds.Tables[0].Rows.Remove(arrRows[0]);
        }

        private void dgv1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            int index=0;
            foreach (DataGridViewRow dr in this.dgv1.Rows)
            {
                this.dgv1.Rows[index].Cells[0].Value = index+1;
                index++;
            }
        }

        private void btnUser_Click(object sender, EventArgs e)
        {
            dgv1.Visible = false;
            user_Dgv.Visible = true;
            WXWS.wx_wsSoapClient client = new WXWS.wx_wsSoapClient();
            string strSql = "select realToUserName,realFromUserName,fromUserName,subscribe,sex,age,location,createTime from MpUserInfo order by createTime desc";
            userDS = client.GetUserDs(strSql);
            user_Dgv.DataSource = userDS.Tables[0];
        }

        private void user_Dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            int index = 0;
            foreach (DataGridViewRow dr in this.user_Dgv.Rows)
            {
                string imgFile = exePath+"\\MpIcon\\"+userDS.Tables[0].Rows[index]["fromUserName"].ToString()+".jpg";
                if (File.Exists(imgFile))
                {
                    this.user_Dgv.Rows[index].Cells["userIcon"].Value = Image.FromFile(imgFile);
                }
                else
                {
                    this.user_Dgv.Rows[index].Cells["userIcon"].Value =null;
                }
                this.user_Dgv.Rows[index].Cells[0].Value = index + 1;
                index++;
            }
        }

        private void user_Dgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            userRowIndex = e.RowIndex;
            WXWS.MpUserInfo user = new WXWS.MpUserInfo();
            user.realToUserName = (string)user_Dgv.Rows[e.RowIndex].Cells["realToUserName"].Value;
            user.realFromUserName = user_Dgv.Rows[e.RowIndex].Cells["realFromUserName"].Value == DBNull.Value ? "" : (string)user_Dgv.Rows[e.RowIndex].Cells["realFromUserName"].Value;
            user.fromUserName = (string)user_Dgv.Rows[e.RowIndex].Cells["fromUserName"].Value;
            user.subscribe = (string)user_Dgv.Rows[e.RowIndex].Cells["subscribe"].Value;
            user.sex = user_Dgv.Rows[e.RowIndex].Cells["sex"].Value == DBNull.Value ? "" : (string)user_Dgv.Rows[e.RowIndex].Cells["sex"].Value;

            user.age = user_Dgv.Rows[e.RowIndex].Cells["age"].Value == DBNull.Value ? 0 : (int)user_Dgv.Rows[e.RowIndex].Cells["age"].Value;
            user.location = user_Dgv.Rows[e.RowIndex].Cells["location"].Value == DBNull.Value ? "" : (string)user_Dgv.Rows[e.RowIndex].Cells["location"].Value;
            user.createTime = user_Dgv.Rows[e.RowIndex].Cells["createTime"].Value.ToString();

            wxuserFrm userFrm = new wxuserFrm(user);
            if (DialogResult.OK == userFrm.ShowDialog())
            {
                userFrm.Close();
                UpdateUerDgv(user);
            }
        }

        private void UpdateUerDgv(WXWS.MpUserInfo user)
        {
            this.user_Dgv.Rows[userRowIndex].Cells["age"].Value = user.age;
            this.user_Dgv.Rows[userRowIndex].Cells["sex"].Value = user.sex;
            this.user_Dgv.Rows[userRowIndex].Cells["realFromUserName"].Value = user.realFromUserName;
            this.user_Dgv.Rows[userRowIndex].Cells["location"].Value = user.location;
            string imgFile = exePath+"\\MpIcon\\" + userDS.Tables[0].Rows[userRowIndex]["fromUserName"].ToString() + ".jpg";
            if (File.Exists(imgFile))
            {
                this.user_Dgv.Rows[userRowIndex].Cells["userIcon"].Value = Image.FromFile(imgFile);
            }
            else
            {
                this.user_Dgv.Rows[userRowIndex].Cells["userIcon"].Value = null;
            }
        }
    }
}